import requests
from bs4 import BeautifulSoup
import traceback
import re
import matplotlib.pyplot as plt
import pandas as pd
import csv

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
}
url = 'https://xueqiu.com/service/v5/stock/screener/quote/list?page=1&size=30&order=desc&order_by=amount&exchange=CN&market=CN&type=sha&_=1641730868838'
response = requests.get(url=url, headers=headers)
json_data = response.json()

data_list = json_data['data']['list']
title = ['股票代码', '股票名称', '当前价', '涨跌额', '涨跌幅', '年初至今', '成交量', '成交额', '换手率', '市盈率(TTM)', '股息率', '市值']
all_data = []
for data in data_list:
    insert = [data['symbol'], data['name'], data['current'], data['chg'],
              data['percent'], data['current_year_percent'], data['volume'],
              data['amount'], data['turnover_rate'], data['pe_ttm'],
              data['dividend_yield'], data['market_capital']]
    all_data.append(insert)
print(data)

output_csv = pd.DataFrame(all_data, columns=title)
output_csv.to_csv('./data_vastar.csv', index=None)